package rickyGame.net
{
	import flash.geom.Point;
	
	import ricky.game.Actor;
	import ricky.game.Pu;
	import ricky.game.action.Act2D;
	import ricky.scene.Scene;
	
	import rickyGame.game.Fighter;
	import rickyGame.game.Player;
	import rickyGame.game.skill.SkillData;
	import rickyGame.server.ServerWorld;
	import rickyGame.server.SpellController;

	/**
	 * 向服务端发送消息
	 */
	public dynamic class ServerHandler
	{
		public function ServerHandler()
		{
			gameWorld=ServerWorld.inst;
		}
		
		/**
		 * 服务端发送消息</br>
		 * 参数:</br>
		 * <b>p:gameObject</b> 玩家对象
		 * <b>gridX:int</b>x格子坐标
		 * <b>gridY:int</b>y格子坐标
		 */
		public function sendPositionToServer(p:Pu,gridX:int,gridY:int):void
		{
			gameWorld.broadcastAround(p);
		}
		
		
		/**
		 * 客户端请求登陆</br>
		 * 参数:</br>
		 *<b>p:player</b> 角色实例
		 */
		public function login(p:Player):void
		{
			//服务端登陆处理
			gameWorld.login(p);//加载数据
		}	
		
		/**
		 * 客户端场景加载完成</br>
		 * 参数:</br>
		 * <b>scene:Scene</b> 场景对象
		 */
		public function onSceneLoaded(scene:Scene):void
		{
			//通知服务端当前场景加载完成
			this.gameWorld.sceneLoaded(scene);
		}
		/**
		* 技能攻击目标</br>
		* 参数:</br>
		* <b>Player:Player</b> 玩家对象</br>
		* <b>abilityID:String 技能id</b>
		* <b>point:Point 目标坐标</b></br>
		* <b>target:Pu 目标</b></br>
		 */
		public function abilityTarget(p:Fighter,abilityID:String,point:Point=null,target:Fighter=null):void
		{
//			JDebug.trace("2:服务端开始计算战斗 fight["+p+"]，目标=["+target);
			p.fightPro.spelling=true;
			if(!p.fightCon)
				p.fightCon=new SpellController(p);
			var skill:SkillData;
			
			if(abilityID=="attack1")
				skill=p.skills.attack1;
			else if(abilityID=="attack2")
				skill=p.skills.attack1;
			else
				skill=p.skills.getSkill(abilityID);
				
			if(skill==null)
				skill=p.skills.attack1;
			
			p.fightCon.spell(target,point,skill);
		}
		
		/**
		 * 给目标对象安装状态魔法</br> 
		 * 参数：</br>
		 * actor:Player 施法者</br>
		 * target:Array 目标</br>
		 * skill:SkillData 技能数据
		 */
		public function skillEffectTargets(actor:Fighter,target:Array,skill:SkillData):void
		{
//			JDebug.trace("服务端开始j计算安装状态魔法");
//			if(!actor.fightCon)
//				actor.fightCon=new SpellController(actor);
			var act:Act2D=SpellController.installState(actor,skill,target);
			SpellController.sendInstallStateToClient(act);
		}
		
		/**
		 * 使用物品</br>
		 * 参数：</br>
		 * actor:Fighter 使用者</br>
		 * target:Fighter 目标</br>
		 */
		public function useGoods(actor:Fighter,target:Fighter):void
		{
			if(!actor.fightCon)
				actor.fightCon=new SpellController(actor);

			actor.fightCon.useGoods(target,null);//
//			actor.fightCon.sendInstallStateToClient(act);
		}

		/**
		 * 游戏服务端口模拟添加怪物</br>
		 * 参数:</br>
		 * <b>count:uint=50</b> 添加数量
		 */
		public function addMonster(count:uint=50):void
		{
			gameWorld.addMonster(count);
		}
			
		/**
		 * 服务网端口模拟的游戏世界
		 */
		private var gameWorld:ServerWorld;
	}
}